package com.moody.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.BankCustDepositDAO;
import com.moody.hibernate.dao.BankCustGuaranteeDAO;
import com.moody.hibernate.dao.BankCustLoanDAO;
import com.moody.hibernate.dao.CustAddressDAO;
import com.moody.hibernate.dao.CustEventsDAO;
import com.moody.hibernate.dao.CustInfoDAO;
import com.moody.hibernate.dao.CustManagerDAO;
import com.moody.hibernate.dao.CustStockholderDAO;
import com.moody.hibernate.domain.BankCustDeposit;
import com.moody.hibernate.domain.BankCustGuarantee;
import com.moody.hibernate.domain.BankCustLoan;
import com.moody.hibernate.domain.CustAddress;
import com.moody.hibernate.domain.CustEvents;
import com.moody.hibernate.domain.CustInfo;
import com.moody.hibernate.domain.CustManager;
import com.moody.hibernate.domain.CustStockholder;
import com.moody.service.DispatchService;
/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-9-2 下午02:11:06
 * @Modified by: 
 * @Description: 
 */
@Service
public class DispatchServiceImpl implements DispatchService {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(DispatchServiceImpl.class);
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	
	private CustAddressDAO custAddressDao = CustAddressDAO.getFromApplicationContext(ctx);
	private CustInfoDAO custInfoDao = CustInfoDAO.getFromApplicationContext(ctx);
	private CustManagerDAO custManagerDao = CustManagerDAO.getFromApplicationContext(ctx);
	private CustStockholderDAO custStockholderDao = CustStockholderDAO.getFromApplicationContext(ctx);
	private CustEventsDAO custEventsDao = CustEventsDAO.getFromApplicationContext(ctx);
	private BankCustDepositDAO bankCustDepositDAO = BankCustDepositDAO.getFromApplicationContext(ctx);
	private BankCustLoanDAO bankCustLoanDAO = BankCustLoanDAO.getFromApplicationContext(ctx);
	private BankCustGuaranteeDAO bankCustGuaranteeDAO = BankCustGuaranteeDAO.getFromApplicationContext(ctx);
	
	public CustInfo getCustomerInfo(String custId) {
		if(custInfoDao.findById(custId)!= null)
			return custInfoDao.findById(custId);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<CustAddress> getCustomerAddress(String custId) {
		if(custAddressDao.findByCustId(custId)!=null)
			return custAddressDao.findByCustId(custId);
		else 
			return null;
	}

	public CustInfo getCustomerOperation(String custId) {
		if(custInfoDao.findById(custId) != null && custInfoDao.findById(custId).getBusiScope()!=null &&
				!custInfoDao.findById(custId).getBusiScope().equals(""))
			return  custInfoDao.findById(custId);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<CustManager> getCustomerManager(String custId) {
		if(custManagerDao.findByCustId(custId)!=null)
			return custManagerDao.findByCustId(custId);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<CustEvents> getCustomerEvents(String custId) {
		if(custEventsDao.findByCustId(custId)!=null)
			return custEventsDao.findByCustId(custId);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<BankCustDeposit> getCustomerDeposit(String custId) {
		if(bankCustDepositDAO.findByCustId(custId)!=null)
			return bankCustDepositDAO.findByCustId(custId);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<BankCustLoan> getCustomerLoan(String custId) {
		if(bankCustLoanDAO.findByCustId(custId)!=null)
			return bankCustLoanDAO.findByCustId(custId);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<BankCustGuarantee> getCustomerGuarantee(String custId) {
		if(bankCustGuaranteeDAO.findByCustId(custId)!=null)
			return bankCustGuaranteeDAO.findByCustId(custId);
		else 
			return null;
	}


	@SuppressWarnings("unchecked")
	public List<CustStockholder> getCustomerStockholder(String custId) {
		List<CustStockholder> list  = null;
		if(custStockholderDao.findByCustId(custId)!=null){
			list = custStockholderDao.findByCustId(custId);
			DecimalFormat df = new DecimalFormat("###,##0.00");
			for(CustStockholder c : list){
				c.setAmountString(df.format(c.getAmount()));
			}
		}
		return list;
	}
}
